<?php

declare(strict_types=1);

namespace App\CustHub\Controller;

use App\CustHub\Request\WxOauthRequest;
use App\CustHub\Service\WxOauthService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\PostMapping;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ResponseInterface;

/**
 * 微信授权登录
 */
#[Controller(prefix: 'ch/wx')]
class WxOauthController extends BaseController
{
    #[Inject]
    protected WxOauthService $oauthService;

    /**
     * 微信登录二维码
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws \RedisException
     */
    #[PostMapping('getLoginQrCode')]
    public function getLoginQrCode(): ResponseInterface
    {
        $qrCode = $this->oauthService->getLoginQrCode();
        return $this->success('微信登录二维码',$qrCode);
    }

    /**
     * 检查二维码登录状态
     * @param WxOauthRequest $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws \RedisException
     */
    #[PostMapping(path: 'checkQrLogin')]
    public function checkQrLogin(WxOauthRequest $request): ResponseInterface
    {
        $result = $this->oauthService->checkQrLogin($request->validated());
        return $this->success($result['msg'],$result);
    }

    /**
     * 扫码登录，如已绑定手机号则验证用户信息并返回用户数据
     * @param WxOauthRequest $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws \RedisException
     */
    #[PostMapping(path: 'checkUser')]
    public function checkUser(WxOauthRequest $request): ResponseInterface
    {
        return $this->success('登录成功',$this->oauthService->checkUser($request->validated()));
    }

    /**
     * 绑定手机号
     * @param WxOauthRequest $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws \RedisException
     */
    #[PostMapping(path: 'bindMobile')]
    public function bindMobile(WxOauthRequest $request): ResponseInterface
    {
        return $this->success('绑定成功',$this->oauthService->bindMobile($request->all()));
    }
}